package org.cmroot.certmaster.service;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

/**
 * Copyright (c) 2011 Andrey Domas <andrey.domas at gmail.com>.
 * *
 * This file is part of certmaster.
 * *
 * Certmaster is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * *
 * Certmaster is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see http://www.gnu.org/licenses
 * or write to the Free Software Foundation,Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301  USA
 */
public class OidDecoder {

    public static final String oldAuthorityKeyIdentifier = "2.5.29.1";
    public static final String oldPrimaryKeyAttributes = "2.5.29.2";
    public static final String certificatePolicies2 = "2.5.29.3";
    public static final String primaryKeyUsageRestriction = "2.5.29.4";
    public static final String subjectDirectoryAttributes = "2.5.29.9";
    public static final String subjectKeyIdentifier = "2.5.29.14";
    public static final String keyUsage = "2.5.29.15";
    public static final String privateKeyUsagePeriod = "2.5.29.16";
    public static final String subjectAlternativeName = "2.5.29.17";
    public static final String issuerAlternativeName = "2.5.29.18";
    public static final String basicConstraints = "2.5.29.19";
    public static final String crlNumber = "2.5.29.20";
    public static final String reasonCode = "2.5.29.21";
    public static final String holdInstructionCode = "2.5.29.23";
    public static final String invalidityDate = "2.5.29.24";
    public static final String deltaCRLindicator = "2.5.29.27";
    public static final String issuingDistributionPoint = "2.5.29.28";
    public static final String certificateIssuer = "2.5.29.29";
    public static final String nameConstraints = "2.5.29.30";
    public static final String crlDistributionPoints = "2.5.29.31";
    public static final String certificatePolicies = "2.5.29.32";
    public static final String policyMappings = "2.5.29.33";
    public static final String authorityKeyIdentifier = "2.5.29.35";
    public static final String policyConstraints = "2.5.29.36";
    public static final String extendedKeyUsage = "2.5.29.37";
    public static final String freshestCRL = "2.5.29.46";
    public static final String x509version3certificateextension = "2.5.29.54";
    public static final String netscapecertificatetype = "2.16.840.1.113730.1.1";
    public static final String baseURL = "2.16.840.1.113730.1.2";
    public static final String revocationURL = "2.16.840.1.113730.1.3";
    public static final String caRevocationURL = "2.16.840.1.113730.1.4";
    public static final String renewalURL = "2.16.840.1.113730.1.7";
    public static final String netscapeCApolicyURL = "2.16.840.1.113730.1.8";
    public static final String sslServerName = "2.16.840.1.113730.1.12";
    public static final String netscapeCertificateComment = "2.16.840.1.113730.1.13";
    public static final String authorityInfoAccess = "1.3.6.1.5.5.7.1.1";
    public static final String biometricInfo = "1.3.6.1.5.5.7.1.2";
    public static final String qcStatements = "1.3.6.1.5.5.7.1.3";
    public static final String logotype = "1.3.6.1.5.5.7.1.12";
    public static final String OCSP = "1.3.6.1.5.5.7.48.1";
    public static final String caIssuers = "1.3.6.1.5.5.7.48.2";
    public static final String timestamping = "1.3.6.1.5.5.7.48.3";
    public static final String id_kp_serverAuth = "1.3.6.1.5.5.7.3.1";
    public static final String id_kp_clientAuth = "1.3.6.1.5.5.7.3.2";
    public static final String id_kp_codeSigning = "1.3.6.1.5.5.7.3.3";
    public static final String id_kp_emailProtection = "1.3.6.1.5.5.7.3.4";
    public static final String id_kp_ipsecEndSystem = "1.3.6.1.5.5.7.3.5";
    public static final String id_kp_ipsecTunnel = "1.3.6.1.5.5.7.3.6";
    public static final String id_kp_ipsecUser = "1.3.6.1.5.5.7.3.7";
    public static final String id_kp_timeStamping = "1.3.6.1.5.5.7.3.8";
    public static final String OCSPSigning = "1.3.6.1.5.5.7.3.9";
    public static final String nsSGC = "2.16.840.1.113730.4.1";
    public static final String msSGC = "1.3.6.1.4.1.311.10.3.3";

    private static Hashtable<String, String> oidLookup = new Hashtable<String, String>();

    static {
        oidLookup.put(oldAuthorityKeyIdentifier, "old Authority Key Identifier");
        oidLookup.put(oldPrimaryKeyAttributes, "old Primary Key Attributes");
        oidLookup.put(certificatePolicies2, "Certificate Policies");
        oidLookup.put(primaryKeyUsageRestriction, "Primary Key Usage Restriction");
        oidLookup.put(subjectDirectoryAttributes, "Subject Directory Attributes");
        oidLookup.put(subjectKeyIdentifier, "Subject Key Identifier");
        oidLookup.put(keyUsage, "Key Usage");
        oidLookup.put(privateKeyUsagePeriod, "Private Key Usage Period");
        oidLookup.put(subjectAlternativeName, "Subject Alternative Name");
        oidLookup.put(issuerAlternativeName, "Issuer Alternative Name");
        oidLookup.put(basicConstraints, "Basic Constraints");
        oidLookup.put(crlNumber, "CRL Number");
        oidLookup.put(reasonCode, "Reason code");
        oidLookup.put(holdInstructionCode, "Hold Instruction Code");
        oidLookup.put(invalidityDate, "Invalidity Date");
        oidLookup.put(deltaCRLindicator, "Delta CRL indicator");
        oidLookup.put(issuingDistributionPoint, "Issuing Distribution Point");
        oidLookup.put(certificateIssuer, "Certificate Issuer");
        oidLookup.put(nameConstraints, "Name Constraints");
        oidLookup.put(crlDistributionPoints, "CRL Distribution Points");
        oidLookup.put(certificatePolicies, "Certificate Policies");
        oidLookup.put(policyMappings, "Policy Mappings");
        oidLookup.put(authorityKeyIdentifier, "Authority Key Identifier");
        oidLookup.put(policyConstraints, "Policy Constraints");
        oidLookup.put(extendedKeyUsage, "Extended key usage");
        oidLookup.put(freshestCRL, "FreshestCRL");
        oidLookup.put(x509version3certificateextension, "X.509 version 3 certificate extension");
        oidLookup.put(netscapecertificatetype, "Netscape certificate type");
        oidLookup.put(baseURL, "Base URL");
        oidLookup.put(revocationURL, "Revocation URL");
        oidLookup.put(caRevocationURL, "CA Revocation URL");
        oidLookup.put(renewalURL, "Renewal URL");
        oidLookup.put(netscapeCApolicyURL, "Netscape CA policy URL");
        oidLookup.put(sslServerName, "SSL server name");
        oidLookup.put(netscapeCertificateComment, "Netscape certificate comment");
        oidLookup.put(authorityInfoAccess, "Authority Information Access");
        oidLookup.put(biometricInfo, "Biometric Info");
        oidLookup.put(qcStatements, "qcStatements");
        oidLookup.put(logotype, "Logotype");
        oidLookup.put(OCSP, "OCSP");
        oidLookup.put(caIssuers, "CA Issuers");
        oidLookup.put(timestamping, "AD Time Stamping");
        oidLookup.put(id_kp_serverAuth, "serverAuth");
        oidLookup.put(id_kp_clientAuth, "clientAuth");
        oidLookup.put(id_kp_codeSigning, "codeSigning");
        oidLookup.put(id_kp_emailProtection, "emailProtection");
        oidLookup.put(id_kp_ipsecEndSystem, "ipsecEndSystem");
        oidLookup.put(id_kp_ipsecTunnel, "ipsecTunnel");
        oidLookup.put(id_kp_ipsecUser, "ipsecUser");
        oidLookup.put(id_kp_timeStamping, "timeStamping");
        oidLookup.put(OCSPSigning, "OCSPSigning");
        oidLookup.put(msSGC, "Microsoft Server Gated Crypto");
        oidLookup.put(nsSGC, "Netscape Server Gated Crypto");

    }

    public static String lookup(String oid) {
        return oidLookup.get(oid);
    }

    public static List<String> decodeKeyUsage(boolean keyUsage[]) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < keyUsage.length; i++) {
            if (keyUsage[i]) {
                switch (i) {
                    case 0:
                        list.add("digitalSignature");
                        break;
                    case 1:
                        list.add("nonRepudiation");
                        break;
                    case 2:
                        list.add("keyEncipherment");
                        break;
                    case 3:
                        list.add("dataEncipherment");
                        break;
                    case 4:
                        list.add("keyAgreement");
                        break;
                    case 5:
                        list.add("keyCertSign");
                        break;
                    case 6:
                        list.add("cRLSign");
                        break;
                    case 7:
                        list.add("encipherOnly");
                        break;
                    case 8:
                        list.add("decipherOnly");
                        break;
                }
            }
        }
        return list;
    }

}
